package g

import (
	"log"

	"git.oschina.net/gunsluo/hawkeye-common/client"
	"git.oschina.net/gunsluo/hawkeye-common/model"
)

var agentClient *client.AgentClient

/*
	Description: 函数功能
	1、创建新上报数据client

	Author: jerrylou, <gunsluo@gmail.com>
	Since:  2016/08/25
*/
func InitAgentClient() {

	if !Config().Agent.Enabled {
		return
	}

	if Config().Agent.Addr == "" {
		return
	}

	agentClient = client.NewAgentClient(Config().Agent.Addr, Config().Agent.Timeout)
}

/*
	Description: 函数功能
	1、发送上报数据给agent

	Author: jerrylou, <gunsluo@gmail.com>
	Since:  2016/08/25
*/
func SendToAgent(metrics []*model.MetricValue) {

	debug := Config().Debug

	if debug {
		log.Printf("=> <Total=%d> %v\n", len(metrics), metrics[0])
	}

	err := agentClient.Send(metrics)
	if err != nil {
		log.Fatalln("send metrics failed.", err.Error())
	}
}
